<template>
<div class="app-container calendar-list-container">
  <div class="filter-container">
    <el-button class="filter-item"  v-if="userManager_btn_add"  style="margin-left: 10px;" @click="handleCreate" type="primary" icon="edit">添加</el-button>
  </div>
  <el-table :key='tableKey' :data="list" v-loading.body="listLoading" border fit highlight-current-row style="width: 100%">
    <el-table-column align="center" label="编号" width="65"> <template slot-scope="scope">
          <span>{{scope.row.id}}</span>
        </template> </el-table-column>
    <el-table-column width="200" align="center" label="姓名"> <template slot-scope="scope">
        <span>{{scope.row.name}}</span>
      </template> </el-table-column>
    <el-table-column width="200" align="center" label="电话"> <template slot-scope="scope">
            <span>{{scope.row.phone}}</span>
          </template> </el-table-column>
    <el-table-column width="180" align="center" label="角色"> <template slot-scope="scope">
          <span>{{scope.row.roleName}}</span>
        </template> </el-table-column>
    <el-table-column width="180" align="center" label="账号状态"> <template slot-scope="scope">
        <el-tag v-if="scope.row.limitedLogin" type="danger">已禁用</el-tag>
        <el-tag v-else>正常</el-tag>
        </template> </el-table-column>
    <el-table-column width="180" align="center" label="最后登录时间"> <template slot-scope="scope">
          <span>{{!scope.row.lastLoginTime?'---': scope.row.lastLoginTime}}</span>
        </template> </el-table-column>
    <el-table-column align="center" label="操作" width="250" fixed="right" > <template slot-scope="scope"> 
        <el-button  size="small" type="success" @click="handleUpdate(scope.row)">编辑
        </el-button>
        <el-button  size="small"  type="primary" @click="limitLoginAction(scope.row, false)" v-if="scope.row.limitedLogin">  启用</el-button>
        <el-button  size="small" type="danger" @click="limitLoginAction(scope.row, true)" v-if="!scope.row.limitedLogin">  禁用</el-button>
        
      </template> </el-table-column>
  </el-table>
  <div v-show="!listLoading" class="pagination-container">
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.pageNo" :page-sizes="[10,20,30, 50]" :page-size="listQuery.pageSize" layout="total, sizes, prev, pager, next, jumper"  :total="total"> </el-pagination>
  </div>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
    <el-form :model="form" :rules="rules" ref="form" label-width="100px">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="form.name" placeholder="请输入姓名"></el-input>
      </el-form-item>
      <el-form-item label="手机" prop="phone">
        <el-input v-if="dialogStatus == 'create'" v-model="form.phone" placeholder="请输入手机号"></el-input>
        <el-input v-else v-model="form.phone" placeholder="请输入手机号" readonly></el-input>
      </el-form-item>
    
      <el-form-item label="性别">
        <el-select class="filter-item" v-model="form.gender" placeholder="请选择">
          <el-option v-for="item in  sexOptions" :key="item.gender" :label="item.name" :value="item.gender"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="角色">
        <el-select class="filter-item" v-model="form.roleId" placeholder="请选择角色"  >
          <el-option v-for="item in  roleList" :key="item.roleId" :label="item.roleName" :value="item.roleId"> </el-option>
        </el-select>
      </el-form-item>  
      
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel('form')">取 消</el-button>
      <el-button v-if="dialogStatus=='create'" type="primary" @click="create('form')">确 定</el-button>
      <el-button v-else type="primary" @click="update('form')">确 定</el-button>
    </div>
  </el-dialog>
</div>
</template>

<script>
import { limitLogin, getObj, staff, addOrUpdate } from "@/api/staff/index";
import { getRoleList } from "@/api/role/index";
import { itemWithSubLevel, subList } from "@/api/district/index";
import { listByUser } from "@/api/order/index";

import { mapGetters } from "vuex";
export default {
  name: "user",
  data() {
    return {
      form: {
        roleId: 0,
        dtlAddress: "",
        province: "",
        phone: "",
        name: "",
        gender: 0,
        password: "123456",
        description: ""
      },
      rules: {
        name: [
          {
            required: true,
            message: "请输入姓名",
            trigger: "blur"
          }
        ],
        phone: [
          {
            required: true,
            message: "请输入手机号",
            trigger: "blur"
          }
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur"
          },
          {
            min: 5,
            max: 20,
            message: "长度在 5 到 20 个字符",
            trigger: "blur"
          }
        ]
      },
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        pageNo: 1,
        pageSize: 20,
        keyword: undefined
      },
      sexOptions: [
        {
          name: "男",
          gender: 0
        },
        {
          name: "女",
          gender: 1
        }
      ],
      dialogFormVisible: false,
      dialogStatus: "",
      userManager_btn_edit: true,
      userManager_btn_del: true,
      userManager_btn_add: true,
      textMap: {
        update: "编辑",
        create: "创建"
      },
      tableKey: 0,
      roleList: []
    };
  },
  mounted() {},
  created() {
    this.getRoleListAction();
    this.getList();
    this.userManager_btn_edit = true; //this.elements["userManager:btn_edit"];
    this.userManager_btn_del = true; //this.elements["userManager:btn_del"];
    this.userManager_btn_add = true; //this.elements["userManager:btn_add"];
  },
  computed: {
    ...mapGetters(["elements"])
  },
  methods: {
    limitLoginAction(item, limit) {
      var param = {
        userId: item.id,
        limit: limit
      };
      var tip = limit?"禁用":"启用"
      this.$confirm("确认执行【"+tip+"】操作吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        limitLogin(param).then(resp => {
          this.getList();
        });
      });
    },
    getRoleListAction() {
      getRoleList().then(resp => {
        var roleList = resp.data.records;
        if (roleList) {
          this.form.roleId = roleList[0].roleId;
        }
        this.roleList = roleList;
      });
    },
    change() {
      console.log(this.province);
    },
    handleFrom(item) {
      return item.from == 0 ? "平台录入" : "用户注册";
    },
    getList() {
      staff(this.listQuery).then(response => {
        this.list = response.data.records;
        this.total = response.data.total;
        this.listLoading = false;
      });
    },
    handleFilter() {
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNo = val;
      this.getList();
    },
    handleCreate() {
      this.resetTemp();
      this.dialogStatus = "create";
      this.dialogFormVisible = true;
    },
    handleUpdate(row) {
      getObj(row.id).then(response => {
        this.form = response.data;
        this.form.lastRoleId = this.form.roleId;
        this.dialogFormVisible = true;
        this.dialogStatus = "update";
      });
    },
    handleDelete(row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        delObj(row.id).then(() => {
          this.$notify({
            title: "成功",
            message: "删除成功",
            type: "success",
            duration: 2000
          });
          const index = this.list.indexOf(row);
          this.list.splice(index, 1);
        });
      });
    },
    create(formName) {
      addOrUpdate(this.form).then(() => {
        this.dialogFormVisible = false;
        this.getList();
        this.$notify({
          title: "成功",
          message: "创建成功",
          type: "success",
          duration: 2000
        });
      });
    },
    cancel(formName) {
      this.dialogFormVisible = false;
      this.$refs[formName].resetFields();
    },
    update(formName) {
      this.create(formName);
    },
    resetTemp() {
      this.form = {
        phone: undefined,
        name: undefined,
        sex: 0,
        password: undefined,
        description: undefined
      };
    }
  }
};
</script>
